package com.jy.api.controller.clerk;

import com.jy.api.dao.OrderDao;
import com.jy.api.domain.*;
import com.jy.api.service.AmountNotMatchException;
import com.jy.api.service.OrderService;
import com.jy.api.service.PaymentService;
import com.jy.api.signUpReq.pos.PaymentSignUpReq;
import com.jy.api.util.UuidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/23.
 */

@Slf4j
@RestController
@RequestMapping(value = "v1/clerk", produces = "application/json;charset=utf-8")
public class ClerkScanPayController {
    private OrderDao orderService;
    private PaymentService paymentService;
    @Autowired
    private OrderService service;

    /**
     * 订单支付（这个是clerk端扫码支付）
     *
     * @param
     * @param paymentSignUpReq
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/scanPayments")
    public ResponseEntity addScanPayments(@RequestBody PaymentSignUpReq paymentSignUpReq) {

        Map<String, Object> orderData = new HashMap<>();
        orderData.put("notes", paymentSignUpReq.getNotes());
        service.patch(paymentSignUpReq.getOrderId(), orderData);
        paymentSignUpReq.setSource("CLERK");
        if (paymentSignUpReq.getChannel().equals(PaymentChannel.PAYCARD)) {
            SwiftPassMicroPayBaseResult result = paymentService.microPay(paymentSignUpReq.getSource(), paymentSignUpReq.getOrderId(), paymentSignUpReq.getAuthCode());
            if (result.isSuccess()) {
                Order order = orderService.findOne(paymentSignUpReq.getOrderId());
                return ResponseEntity.ok().body(order);
            } else if (result.isNeedQuery()) {
                return ResponseEntity.accepted().build();
            } else if (result.isFailed()) {
                Order order = orderService.findOne(paymentSignUpReq.getOrderId());
                Order newOrder = new Order(order);
                order.setStatus(OrderStatus.CANCELED);
                order.setEnabled(false);
                order.removeAllOrderItem();
                service.saveOrder(order);
                service.saveOrder(newOrder);
                return ResponseEntity.badRequest().body("支付失败，请重新进行支付！");
            } else {
                throw new RuntimeException("支付结果未知，请稍后到订单列表查看支付状态");
            }
        } else if (paymentSignUpReq.getChannel().equals(PaymentChannel.CASH) || paymentSignUpReq.getChannel().equals(PaymentChannel.ALIPAY_CASH) || paymentSignUpReq.getChannel().equals(PaymentChannel.WECHATPAY_CASH)) {
            try {
                paymentService.cashPay(paymentSignUpReq.getSource(), paymentSignUpReq.getOrderId(), paymentSignUpReq.getAmount(), paymentSignUpReq.getChannel());
                Order order = orderService.findOne(paymentSignUpReq.getOrderId());
                return ResponseEntity.ok().body(order);
            } catch (AmountNotMatchException e) {
                return ResponseEntity.badRequest().body("支付金额与订单金额不匹配，请检查");
            }

        } else if (paymentSignUpReq.getChannel().equals(PaymentChannel.MEMBER)) {
            try {
                paymentService.memberPay(paymentSignUpReq.getSource(), paymentSignUpReq.getOrderId(), paymentSignUpReq.getAmount());
                Order order = orderService.findOne(paymentSignUpReq.getOrderId());
                return ResponseEntity.ok().body(order);
            } catch (MemberAccountNotEnoughException e) {
                return ResponseEntity.badRequest().body("会员账户余额不足，请提醒顾客充值");
            } catch (AmountNotMatchException e) {
                return ResponseEntity.badRequest().body("支付金额与订单金额不匹配，请检查");
            }
        } else {
            return ResponseEntity.badRequest().body("未识别的支付方式：" + paymentSignUpReq.getChannel());
        }
    }

    @Autowired
    public void setOrderService(OrderDao orderService) {
        this.orderService = orderService;
    }

    @Autowired
    public void setPaymentService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }
}
